package com.tlb.dto;

import com.tlb.ejb.*;
import com.tlb.dto.ThreadDataView;
import com.tlb.dto.PostView;
import java.util.*;

public class ThreadFacade {

    private int posterId;
    private int threadId;
    private ThreadDataView tdv;
    List<PostView> posts = null;

    public ThreadFacade(int posterId, int threadId) {
        this.posterId = posterId;
        this.threadId = threadId;
        tdv = new ThreadDAO().getThreadData(threadId);
    }

    
    public boolean theadExists() {
        return tdv != null;
    }
    
    public List<PostView> getPosts() {
        if (posts == null) {
            posts = new ThreadDAO().getPosts(posterId, threadId);
        }
        return posts;
    }

    public Date getMinThreadDate() {
        return tdv.getMinThreadDate();
    }

    public Date getMaxThreadDate() {
        return tdv.getMaxThreadDate();
    }

    public boolean getSticky() {
        return tdv.getSticky();
    }

    public boolean getTOS() {
        return tdv.getTos();
    }

    public boolean getNSFW() {
        return tdv.getNsfw();
    }

    public boolean getSpam() {
        return tdv.getSpam();
    }

    public void toggleSticky() {
        StickyDAO sdao = new StickyDAO();
        if (this.getSticky()) {
            new StickyDAO().deleteStickyByThreadId(threadId);
        } else {
            new StickyDAO().insertAdminSticky(threadId, posterId);
        }
    }

    public int getThreadId() {
        return threadId;
    }

    public String getThreadTitle() {
        Iterator it = this.getPosts().iterator();
        PostView firstPost = (PostView) it.next();
        String hdrCont = firstPost.getTitle();
        if (hdrCont == null) {
            hdrCont = "Title";
        }
        hdrCont = hdrCont.replaceAll("</?i>", "");
        hdrCont = hdrCont.replaceAll("<", "&lt; ");
        hdrCont = hdrCont.replaceAll(">", "&gt; ");
        //hdrCont = hdrCont.length() > 50 ? hdrCont.substring(0, 49) + "..." : hdrCont;
        return hdrCont;
    }
}
